package lightObjects;

import java.util.ArrayList;
import java.util.List;

import utils.ColorRGB;
import utils.Point3D;
import utils.Ray;
import utils.Vector;
import main.Parseable;
import main.Parser;
import main.Parser.ParseException;

public class DirectLight implements lightObject, Parseable {

    private ColorRGB color;
    private Vector direction;
    private Vector oppDirection;

    public DirectLight() {
	this.color = new ColorRGB(1, 1, 1);
    }

    public boolean setParameter(String name, String[] args) throws ParseException {
	if (name.equals("color")) {
	    if (args.length != 3) {
		System.out.println("bad parameters");
		return false;
	    }
	    color = new ColorRGB(Parser.stringArrToDoubleArr(args));
	    return true;
	} else if (name.equals("direction")) {
	    if (args.length != 3) {
		System.out.println("bad parameters");
		return false;
	    }
	    direction = new Vector(Parser.stringArrToDoubleArr(args));
	    return true;
	}

	// We didn't find any parameter
	return false;
    }

    public boolean finalizeObject() {
	if (direction == null) {
	    System.out.println("Error: one or more parameters are missing");
	    return false;
	}

	direction = direction.normalize();
	oppDirection = direction.scale(-1);

	return true;
    }

    @Override
    public boolean isCamera() {
	return false;
    }

    public boolean isLight() {
	return true;
    }

    @Override
    public Ray buildRayFrom(Point3D p) {
	return new Ray(p, oppDirection);
    }

    @Override
    public ColorRGB getIllumination(Point3D p) {
	return color;
    }

    public double getDistanceFromPoint(Point3D p) {
	return Double.POSITIVE_INFINITY;
    }

    @Override
    public List<lightObject> getLightSources() {
	List<lightObject> l = new ArrayList<lightObject>();
	l.add(this);
	return l;
    }
}
